#include "drm_core.h"
#include "rgb24_video.h"

/* 显示图片, 根据X,Y坐标 */
int show_rgb_image(struct rgb24_object *rgb, int x ,int y)
{
	int i,j;
	uint32_t word;
	if(x<0 || y<0){
		printf("wrong set !\n");
		return -1;
	}
		
	for(j = 0; j < rgb->pix_height; j++){
		for(i = 0 ; i < rgb->pix_width; i++){
			if((j+y) < buf.height && (i+x)<buf.width){
				word = 0;
				word =  ((word | rgb->frame_buffer[(j*rgb->pix_width+i)*3+0])<<16) | 
						((word | rgb->frame_buffer[(j*rgb->pix_width+i)*3+1])<<8) | 
						((word | rgb->frame_buffer[(j*rgb->pix_width+i)*3+2]));
				buf.vaddr[(j+y)*buf.width+(x+i)] = word;
			}
			else
				continue;
		}
	}

	return 0;	
}

int main(int argc, char **argv)
{
	int i,j;
	int ret;
	struct rgb24_object rgb;

	/* 初始化 DRM */
	ret = drm_init();
	if(ret < 0){
		printf("drm init fail\n");
		return -1;
	}

	/* 初始化RGB */
	ret = rgb24_init(&rgb);
	if (ret < 0) {
		printf("rgb24_init failed.\n");
		exit(0);
	}

	/* 播放视频 */
	while ((ret = rgb24_get_frame(&rgb)) >= 0) {
		printf("image:[%d].\n", ret);  // LOG

		/* 显示画面信息 */
		ret = show_rgb_image(&rgb , 0, 0);
		if(ret < 0) {
			printf("show_bmp wrong!\n");
			goto fail2;
		}
	}

	/* 注销 DRM */
	getchar();
	drm_exit();
		
	/* 销毁 RGB */
	rgb24_deinit(&rgb);

	return 0;

fail2: 
	drm_exit();
	rgb24_deinit(&rgb);
fail1:	
	printf("Proglem run fail,please check !\n");
	return -1;
}